﻿

using IotContract.Enums;
using ProtoBuf;
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IotContract.Dtos.Servers;
/// <summary>
/// 设备点位
/// </summary>
[ProtoContract]
public class EquipTagDto
{
    [ProtoMember(19)]
    public long Id { get; set; }
    /// <summary>
    /// 名称
    [ProtoMember(1)]
    public string Name { get; set; }
    /// <summary>
    /// 点位编号
    /// </summary>
    [ProtoMember(2)]
    public string TagNum { get; set; }
    /// <summary>
    /// 编号属性
    /// </summary>
    [ProtoMember(3)]
    public string TagProperty { get; set; }
    /// <summary>
    /// 地址
    /// </summary>
    [ProtoMember(4)]
    public string Address { get; set; }
    /// <summary>
    /// 数据类型
    /// </summary>
    [ProtoMember(5)]
    public int DataType { get; set; }
    /// <summary>
    /// 数据长度
    /// </summary>
    [ProtoMember(6)]
    public int DataLength { get; set; }

    /// <summary>
    /// 设备id
    /// </summary>
    [ProtoMember(7)]
    public long EquipInfoId { get; set; }
    /// <summary>
    /// 读写
    /// </summary>
    [ProtoMember(8)]
    public int ReadWrite { get; set; }


    /// <summary>
    /// 读取块
    /// </summary>
    [ProtoMember(9)]
    public string ReadBlock { get; set; }
    /// <summary>
    /// 是否启用
    /// </summary>
    [ProtoMember(10)]
    public int IsEnable { get; set; }
    /// <summary>
    /// 表达式
    /// </summary>
    [ProtoMember(11)]
    public string Expression { get; set; }

    /// <summary>
    /// 备注
    /// </summary>
    [ProtoMember(12)]
    public string Remark { get; set; }
    /// <summary>
    /// 默认D=168
    /// </summary>
    [ProtoMember(13)]
    public PlcMemoryType AddrType { get; set; } = PlcMemoryType.D;
    /// <summary>
    /// 值
    /// </summary>
    [ProtoMember(14)]
    public string Value { get; set; }
    /// <summary>
    /// 旧值
    /// </summary>
    [ProtoMember(15)]
    public string OldValue { get; set; }
    /// <summary>
    /// 读取时间
    /// </summary>
    [ProtoMember(16)]
    public DateTime? ReadTime { get; set; }
    /// <summary>
    /// 值变化时间
    /// </summary>
    [ProtoMember(17)]
    public DateTime? ReadChangeTime { get; set; }

    [ProtoMember(18)]
    public bool IsMqttPublish { get; set; }

    /// <summary>
    /// 获取iot地址
    /// </summary>
    /// <returns></returns>
    public (string,int) GetIotAddress()
    {
        var type = (PlcDataType)DataType;
        string addr = string.Empty;
        if (type == PlcDataType.BIT)
        {
            addr = $"DB{ReadBlock}.{Address}.{DataLength}";

            return (addr,1);
        }
        if (type == PlcDataType.BYTE)
        {
            addr = $"DB{ReadBlock}.{Address}";
            return (addr,2);
        }
        if (type == PlcDataType.INT16)
        {
            addr = $"DB{ReadBlock}.{Address}";
            return (addr,4);
        }
        if (type == PlcDataType.INT)
        {
            addr = $"DB{ReadBlock}.{Address}";
            return (addr, 5);
        }
        if (type == PlcDataType.FLOAT)
        {
            addr = $"DB{ReadBlock}.{Address}";
            return (addr, 9);
        }
        if (type == PlcDataType.INT64)
        {
            addr = $"DB{ReadBlock}.{Address}";
            return (addr, 7);
        }
        if (type == PlcDataType.STR)
        {
            addr = $"DB{ReadBlock}.{Address}";
            return (addr, 11);
        }
        return ($"DB{ReadBlock}.{Address}", 5);
    }
}
